#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <unistd.h>
std::mutex m;
std::condition_variable v;

int flag = 0;
void print(int a)
{
    /*简单的使用场景选择lock_guard即可*/
    // std::lock_guard<std::mutex> lock(m);
    std::unique_lock<std::mutex> lock(m); /*根据使用场景选择使用*/
    
    std::cout << a << ":"
              << "hello world"
              << std::endl;
}
void print1()
{
    
}
void print2()
{

    /*轮询*/
    while (flag != 1)
    {
        sleep(3);
    }
}
int main()
{
#if 0
    std::thread t(print, 10);

    // std::thread t2 = t; /*不允许拷贝构造和复制运算符的重载*/
    /*但是可以move，*/
    std::thread t2 = std::move(t);
    // t.detach();
    // if (t.joinable())
    // {
    //     t.join();
    // }
    std::cout
        << std::this_thread::get_id() << std::endl;
    std::cout << t.get_id() << std::endl;
    t2.join();
#endif

    std::thread t[10];
    int idx = 0;
    for (auto &thread : t)
    {
        idx++;
        thread = std::thread(print, idx); /*移动语义*/
    }
    for (auto &thread : t)
    {
        thread.join();
    }

    return 0;
}
